<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
</body>
<script>   
    var maxProfit = function(prices) {
        const len = prices.length;
        let buy = prices[0];
        let profit = 0;

        for(let i=1; i<len; i++){
            if(prices[i] < buy){
                buy = prices[i];
            }
            if(prices[i] > buy && prices[i]-buy > profit){
                profit = prices[i]-buy;
            }
        }
        return profit;
    };
    let stock = [7,1,5,3,6,4];
    console.log(maxProfit(stock));
    
</script>
</html>